<template>
  <div class="flex flex-col h-full">
    <a-card :body-style="{ padding: '8px 12px' }">
      <a-form ref="formRef" :model="formState" class="flex justify-between">
        <div class="flex">
          <a-form-item name="pyjm" class="w-[260px] mr-3 !mb-0">
            <!-- <a-input v-model:value="formState.pyjm" placeholder="姓名/助记码/卡号/手机号/身份证号" /> -->
            <SelectTable
              v-model:value="formState.pyjm"
              :search-params="{ stdate: formState.dateRange?.[0], eddate: formState.dateRange?.[1] }"
              type="输液管理"
              no-page
              placeholder="姓名/助记码/卡号/手机号/身份证号"
              @select="onSelect"
            />
          </a-form-item>
          <a-form-item label="收费时间" name="dateRange" class="!mb-0">
            <a-range-picker v-model:value="formState.dateRange" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
          </a-form-item>
        </div>

        <div>
          <a-button style="margin: 0 8px" @click="onReset">重置</a-button>
          <a-button type="primary" @click="getData">刷新</a-button>
        </div>
      </a-form>
    </a-card>
    <a-card class="mt-2 flex-1">
      <BaseTable :loading="loading" column-code="00001026" :data-source="tableData" fill bordered :pagination="pagination">
        <template #bodyCell="{ column, record }">
          <template v-if="column.dataIndex === 'children'">
            <DoctorAdviceOrder :row-data="record" only-children :params-key="{ mc: 'mc', gg: 'gg', sl: 'jlsl', dw: 'jldw', yf: 'fyff', pl: 'pl' }" />
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" @click="onPrintStickers(record)"> 输液瓶贴 </a-button>
            <a-divider type="vertical" />
            <a-button type="link" size="small"> 预览瓶贴 </a-button>
          </template>
        </template>
      </BaseTable>
    </a-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import BaseTable from '@/components/BaseTable'
// import SearchContainer from '@/components/SearchContainer'
import SelectTable from '@/components/SelectTable'

const formRef = ref(null)
const formState = ref({
  dateRange: [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
})
const { loading, tableData, pagination, getData } = useTable({
  url: '/outp/mzhs/getOutpClinicalOrderInfoSYYZList',
  immediate: true,
  params: {},
  formatParams: () => {
    return {
      stdate: formState.value['dateRange']?.[0],
      eddate: formState.value['dateRange']?.[1],
      jzbh: formState.value.jzbh,
      status: '2',
      tclb: '1'
    }
  },
  formatResult: (result) => {
    result.forEach((i) => (i.advOrderType = 0))
    return result
  }
})

const onSelect = ($event) => {
  formState.value.pyjm = $event.brName
  formState.value.jzbh = $event.jzbh
}

const onReset = () => {
  formRef.value.resetFields()
  getData()
}

// 输液瓶贴
const onPrintStickers = async (row) => {
  for (let i = 0; i < row?.sl; i++) {
    const { code, msg, data } = await post('/outp/outPrint/printInfusionBottleStickers', { yzid: row?.id, children: row?.children, dqsy: i + 1 })
    if (code !== 0) return message.error(msg)
    window.openExe?.rpt(data) //调用winform打印
  }
}
</script>

<style lang="less" scoped>
:deep(.ant-table-row-expand-icon) {
  display: none;
}

:deep(.ant-tabs-content-holder) {
  overflow: hidden;
}
</style>
